
import React from 'react';

/** 帧事件 */
export function useAnimate(fn: () => void) {
    const timer = React.useRef<number>(0)

    const isOk = React.useRef(false)

    React.useEffect(() => {
        isOk.current = true;
        function animate() {
            if (isOk.current === false) return;
            timer.current = requestAnimationFrame(animate);
            fn();
        }
        animate();
        return () => {
            isOk.current = false
            cancelAnimationFrame(timer.current);
        }
    }, [fn])
}